package com.multiplayer.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.multiplayer.dao.IAnnouncementDao;
import com.multiplayer.dao.impl.AnnouncementDaoImpl;
import com.multiplayer.entity.Admin;
import com.multiplayer.entity.Announcement;
import com.multiplayer.tools.PageData;
  
/**
 * Servlet implementation class GongGaoServlet
 */ 
@WebServlet("/GongGaoServlet")
public class GongGaoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    IAnnouncementDao iad = new AnnouncementDaoImpl();   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GongGaoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//得到op的值
		String op = "";
		if(request.getParameter("op")!=null){
			op = request.getParameter("op");
		}
		//如果op的值为all,即得到所有的公告
		System.out.println("op:"+op);
		if("all".equals(op)){
			getAllAnn(request, response);
		}else if("updateGetOne".equals(op)){
			updateGetAnn(request, response);
		}else if("update".equals(op)){
			updateByTid(request, response);
		}else if("delByTid".equals(op)){
			delByTid(request, response);
		}else if("querypage".equals(op)){
			
		}else if("queryAnnByPage".equals(op)){
			queryAnnByPage(request,response);
		}
		
	}
	private void queryAnnByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String keywords="";
		// 2获取请求参数
		if (request.getParameter("keywords") != null) {
			keywords = request.getParameter("keywords");
		}
		// 页码默认值为1
		int page=1;
		int pagesize=3;
		//获取页面传来的值
		if(request.getParameter("page")!=null){
			page=Integer.parseInt(request.getParameter("page"));
		}
		if(request.getParameter("pagesize")!=null){
			pagesize=Integer.parseInt(request.getParameter("pagesize"));
		}
		//调用服务层方法获取根据页面传来的数据得到的PageDate对象
		PageData<Announcement> pd = iad.getAllAnnouncement(page, pagesize, keywords);
		//打包， 界面接收使用
		request.setAttribute("keywords", keywords);
		request.setAttribute("pd", pd);
		System.out.println(page);
		System.out.println(pagesize);
		System.out.println(pd.getData());
		request.getRequestDispatcher("announcement.jsp").forward(request, response);
	}

	private void delByTid(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String announcementId = "";
		if(request.getParameter("tid")!=null){
			announcementId = request.getParameter("tid");
		}
		boolean flag = iad.deleteAnnouncementById(announcementId);
		Gson gson = new Gson();
		String jsonString = gson.toJson(flag);
		//响应
		PrintWriter pw = response.getWriter();
		pw.print(jsonString);
		
		//关闭
		pw.close();
	}

	private void updateByTid(HttpServletRequest request, HttpServletResponse response) throws IOException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
		String tid = request.getParameter("tid");
		String ano = request.getParameter("ano");
		Date tdate = null;
		try {
			tdate = sdf.parse(request.getParameter("tdate"));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String tinfo = request.getParameter("tinfo");
		String tstatus = request.getParameter("tstatus");
		Admin admin = new Admin();
		Announcement announcement = new Announcement(tid, ano, tdate, tinfo, tstatus, admin);
		boolean flag = iad.editAnnouncement(announcement);
		Gson gson = new Gson();
		String jsonString = gson.toJson(flag);
		//响应
		PrintWriter pw = response.getWriter();
		pw.print(jsonString);
		
		//关闭
		pw.close();
	}

	private void updateGetAnn(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String keywords = "";
		if(request.getParameter("keywords")!=null){
			keywords = request.getParameter("tid");
		}
		List<Announcement> list = iad.queryAnnouncementByKeywords(keywords);
		Announcement ann = null;
		if(list!=null){
			ann = list.get(0);
		}
		Gson gson = new Gson();
		String jsonString = gson.toJson(ann);
		//响应
		PrintWriter pw = response.getWriter();
		pw.print(jsonString);
		
		//关闭
		pw.close();
	}
	/**
	 * 得到所有的公告
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void getAllAnn(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int page = 1;
		if(request.getParameter("page")!=null){
			page = Integer.valueOf(request.getParameter("page"));
		}
		int pagesize = 10;
		if(request.getParameter("pagesize")!=null){
			pagesize = Integer.valueOf(request.getParameter("pagesize"));
		}
		String keywords = "";
		if(request.getParameter("keywords")!=null){
			keywords = request.getParameter("keywords");
		}
		PageData<Announcement> pd = iad.getAllAnnouncement(page, pagesize, keywords);
		request.setAttribute("keywords", keywords);
		request.setAttribute("pd", pd);
		request.getRequestDispatcher("GongGao.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
